package com.niit.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.niit.bean.User;
import com.niit.service.UserService;
import com.niit.service.impl.UserServiceImpl;

@WebServlet("/UpdatePassword")
public class UpdatePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String oldpwd = request.getParameter("oldpwd");
		String newpwd1 = request.getParameter("newpwd1");
		String newpwd2 = request.getParameter("newpwd2");
		OutputStream os = response.getOutputStream();
		if(!newpwd1.equals(newpwd2)){
			os.write("两次密码不一致".getBytes());
			return;
		}
		User user = (User) request.getSession().getAttribute("user");
		if(!user.getPassword().equals(oldpwd)){
			os.write("原密码不正确".getBytes());
			return;
		}
		UserService us = new UserServiceImpl();
		user = us.login(user.getUsername(), user.getPassword());
		user.setPassword(newpwd1);
		if(us.update(user)>0){
			os.write("成功".getBytes());
		}
	}

}
